package de.karlNet.sambamanager.tests.login;

import java.io.UnsupportedEncodingException;
import java.util.UUID;

import junit.framework.Assert;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import de.karlNet.login.LoginService;
import de.karlNet.sambamanager.ldapController.commands.RemoveUser;
import de.karlNet.sambamanager.ldapController.commands.macros.CreateGroupMacro;
import de.karlNet.sambamanager.ldapController.commands.macros.CreateUserMacro;
import de.karlNet.sambamanager.ldapModel.LdapUser;
import de.karlNet.sambamanager.ldapModel.dto.LdapGroupCreationDTO;
import de.karlNet.sambamanager.ldapModel.dto.LdapUserCreationDTO;
import de.karlNet.sambamanager.tests.SambamanagerBaseTest;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/applicationContext.xml" })
public class LoginViaService extends SambamanagerBaseTest {
	@Autowired
	private CreateUserMacro createUserMacro;
	@Autowired
	private LoginService loginService;
	@Autowired
	private RemoveUser removeUser;
	@Autowired
	private CreateGroupMacro createGroupMacro;

	@Test
	public void negativeGroupTest() throws UnsupportedEncodingException {
		String username = UUID.randomUUID().toString();
		LdapUserCreationDTO ldapUserCreationDTO = new LdapUserCreationDTO();
		LdapUser ldapUser = ldapUserCreationDTO.getLdapUser();
		ldapUser.setCn(username);
		String dn = "cn=" + username + this.getNestInDN();
		ldapUser.setDn(dn);
		ldapUser.setUsername(username);
		ldapUser.setPassword("p4ssw0rd!");
		this.createUserMacro.createUser(ldapUserCreationDTO);
		LdapGroupCreationDTO creationDTO = new LdapGroupCreationDTO();
		String groupCn = UUID.randomUUID().toString();
		creationDTO.getLdapGroup().setCn(groupCn);
		creationDTO.getLdapGroup().setDn("cn=" + groupCn + this.getNestInDN());
		this.createGroupMacro.createGroup(creationDTO);

		String result = "false";
		result = loginService.checkGroup(username, groupCn);
		this.removeUser.removeUserItself(ldapUser);
		Assert.assertTrue(result.equals("false"));
	}
	
	@Test
	public void testMembeshipService() throws UnsupportedEncodingException {
		String username = UUID.randomUUID().toString();
		LdapUserCreationDTO ldapUserCreationDTO = new LdapUserCreationDTO();
		LdapUser ldapUser = ldapUserCreationDTO.getLdapUser();
		ldapUser.setCn(username);
		String dn = "cn=" + username + this.getNestInDN();
		ldapUser.setDn(dn);
		ldapUser.setUsername(username);
		ldapUser.setPassword("p4ssw0rd!");
		this.createUserMacro.createUser(ldapUserCreationDTO);
		LdapGroupCreationDTO creationDTO = new LdapGroupCreationDTO();
		String groupCn = UUID.randomUUID().toString();
		creationDTO.getLdapGroup().setCn(groupCn);
		creationDTO.getMembers().add(ldapUser);
		creationDTO.getLdapGroup().setDn("cn=" + groupCn + this.getNestInDN());
		this.createGroupMacro.createGroup(creationDTO);

		String result = "false";
		result = loginService.checkGroup(username, groupCn);
		this.removeUser.removeUserItself(ldapUser);
		Assert.assertTrue(result.equals("true"));
	}
	
	@Test
	public void successLoginTest() throws UnsupportedEncodingException {
		String username = UUID.randomUUID().toString();
		LdapUserCreationDTO ldapUserCreationDTO = new LdapUserCreationDTO();
		LdapUser ldapUser = ldapUserCreationDTO.getLdapUser();
		ldapUser.setCn(username);
		String dn = "cn=" + username + this.getNestInDN();
		ldapUser.setDn(dn);
		ldapUser.setUsername(username);
		ldapUser.setPassword("p4ssw0rd!");
		this.createUserMacro.createUser(ldapUserCreationDTO);
		LdapGroupCreationDTO creationDTO = new LdapGroupCreationDTO();
		String groupCn = UUID.randomUUID().toString();
		creationDTO.getLdapGroup().setCn(groupCn);
		creationDTO.getMembers().add(ldapUser);
		creationDTO.getLdapGroup().setDn("cn=" + groupCn + this.getNestInDN());
		this.createGroupMacro.createGroup(creationDTO);

	
		String result = "false";
		result = (loginService.loginWithGroup(username, "p4ssw0rd!",
				groupCn));
		this.removeUser.removeUserItself(ldapUser);
		Assert.assertTrue(result.equals("true"));
	}
}
